<?php
class Family extends AppModel {		 
	var $name = 'Family';
	
	// Define the join relationship between family and event
	var $hasAndBelongsToMany = array('Event', 'Person');
	

	/**
	 * Returns all of the info from the family table for the given ID
	 */
	function getFamilyFromID($fid) {
		$result = $this->find('first', array('conditions' => array('Family.id' => $fid)));
		return $result['Family'];
	}
	
	/**
	 * Return a list of all of the families in the database (for searching)
	 */
	function getAllFamilies() {
		return $this->find('list');
	}

	/**
	 * Returns a list of all people from the given family ID
	 */
	function getPeopleFromFamily($fid) {
		return $this->FamiliesPerson->find('list', array('conditions' => array('FamiliesPerson.family_id' => $fid),
								 'fields' => array('FamiliesPerson.person_id')));		
	}

	/* 
	 * Gets the events for any number of families. Expects an array of family
	 * ids as input 
	 */
	function getEventsFromFamilies($fams = null) {
		if (empty($fams)) {
			return array();
		}
			
		// Loop through each family and add to an array index.
		$reteventsTemp = array();
		foreach($fams as $fam) {
			$arr = $this->find('all', array('conditions' => array('Family.id' => $fam)));
		
			if (!empty($arr)) { 
				$evts = $arr[0]['Event'];
				
	//	I'm using this method to ensure we don't get duplicates.
				foreach($evts as $evt) {
					$reteventsTemp[$evt['id']] = '';
				}
			}
		}
			
		// Get the list into a more standard format
		$retevents = array();
		foreach ($reteventsTemp as $key => $retevent) {
			$retevents[] = $key;
		}
		return $retevents;
	}
	
	/*
	 * Returns whether the given user is an admin of the given family.
	 */
	function isPersonFamilyAdmin($pid, $fid){
		return $this->FamiliesPerson->find('count',
							 array('conditions' =>
							 array('FamiliesPerson.family_id' => $fid,
							 'FamiliesPerson.person_id' => $pid,
							 'FamiliesPerson.admin' => TRUE)
							 )
							 ) > 0;
	}

	/*
	 * Returns an array indexed on user ids with the value either 
	 * 0 (not admin) or 1 (admin)
	 */
	function getFamilyPersonAdminStatus($fid) {
		$temp_results = $this->FamiliesPerson->find('all', 
						array('conditions' => array('FamiliesPerson.family_id' => $fid),
									'fields' => array('FamiliesPerson.admin', 'FamiliesPerson.person_id'))
						);

		$result = array();

		foreach ($temp_results as $temp_result) {
			$result[$temp_result['FamiliesPerson']['person_id']] = $temp_result['FamiliesPerson']['admin'];
		}

		return $result;
	}

	/*
	 * Returns the family name from the given family ID
	 */
	function getFamilyName($fid){
		$res = $this->find('first',
					 array('conditions' => array('id' => $fid),
					 'fields' => array('name')));
		return $res['Family']['name'];
	}

	/*
	 * Returns an array with family names for the given family IDs;
	 * 
	 * The returned array is indexed by the family ids
	 */
	function getFamilyNames($fids) {
		$result = array();

		foreach ($fids as $fid) {
			$result[$fid] = $this->getFamilyName($fid);
		}

		return $result;
	}	

	/*
	 * Returns an array of ids for all families sharing the event $eid
	 */
	function getFamiliesSharingEvent($eid) {
		return $this->EventsFamily->find('list', array('conditions' => array('EventsFamily.event_id' => $eid),
							 'fields' => array('EventsFamily.family_id')));


	}

	/*
	 * Validation for family creation
	 */
	var $validate = array(
			'name' => array(
					'maxLength' => array(
									 'rule' =>	array('maxLength', 50),
									 'required' => true,
									 'allowEmpty' => false,
									 'message' => 'Family name must between 1 and 50 characters'
									 ),
					'isUnique' => array(
									'rule' => 'isUnique',
									'message' => 'This family name has been taken.	Please choose a different family name.'
									),
					'name-regexRule' => array(
									'rule' => '/^[^\\\\\/\"]*$/i',
									'message' => 'Name must not contain /, \\ or "'
									)			 
					)
			);
	}
?>
